package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper,Dish> implements DishService {

    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setMealDishMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        this.save(dish);
        //System.out.println(dish.getId());
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        int flavorsize = flavors.size();
        for(int i=0;i<flavorsize;++i){
            flavors.get(i).setDishId(dishId);
            dishFlavorsMapper.insert(flavors.get(i));
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO>page = dishMapper.pageQuery(dishPageQueryDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }


    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        for(Long id:ids){
            Dish dish = dishMapper.selectById(id);
            if(dish.getStatus() == StatusConstant.ENABLE)//起售中的菜品不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        List<Long> setMealIdByDishIds = setMealDishMapper.getSetMealIdByDishIds(ids);
        //TODO 此处还未测试
        if(setMealIdByDishIds!=null&&setMealIdByDishIds.size()>0)//如果菜品关联了套餐则不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);

        dishMapper.deleteByIds(ids);
        dishFlavorsMapper.deleteByDishIds(ids);
    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        List<DishFlavor> dishFlavorlist = dishFlavorsMapper.getByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorlist);
        return dishVO;
    }

    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

        //跟新口味的数据 为减少麻烦 采取先全部删除后新增的方式
        dishFlavorsMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> dishFlavorlist = dishDTO.getFlavors();
        if(dishFlavorlist!=null) {
            for(int i=0;i<dishFlavorlist.size();++i){
                dishFlavorlist.get(i).setDishId(dishDTO.getId());
                dishFlavorsMapper.insert(dishFlavorlist.get(i));
            }
        }

    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {

        String key = "dish_"+dish.getCategoryId();
        List<DishVO> dishVOList = (List<DishVO>) redisTemplate.opsForValue().get(key);

        if(dishVOList!=null&&dishVOList.size()>0)
            return dishVOList;

        dishVOList = new ArrayList<>();

        List<Dish> dishList = dishMapper.selectListByCategoryId(dish.getCategoryId());
        //List<Dish> dishList = dishMapper.list(dish);
        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorsMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        redisTemplate.opsForValue().set(key,dishVOList);
        return dishVOList;
    }

    @Override
    public List<Dish> getdishesByCategoryId(Long categoryId) {
        LambdaQueryWrapper<Dish>queryWrapper = new LambdaQueryWrapper<>();
        if(categoryId!=null)
            queryWrapper.eq(Dish::getCategoryId,categoryId);
        return this.list(queryWrapper);
    }
}

